﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MonoPhysics
{
    internal class StatisticArbiter
    {
        private RigidBody rb;

        private float lastVX;
        private float lastVY;
        private float lastVAng;

        private int VXtolerence;
        private int VYtolerence;
        private int AngVtolerence;

        private int VXvariations;
        private int VYvariations;
        private int AngVVariations;

        private int frameLimit;
        private int frameTolerence;

        public StatisticArbiter(RigidBody rb, int frameLimit,int frameTolerence)
        {
            this.rb = rb;
            this.frameLimit = frameLimit;
            this.frameTolerence = frameTolerence;
            this.lastVX = rb.PhysicStats.V.X;
            this.lastVY = rb.PhysicStats.V.Y;
            this.lastVAng = rb.PhysicStats.AngularVitess;

            this.VXtolerence = frameTolerence;
            this.VYtolerence = frameTolerence;
            this.AngVtolerence = frameTolerence;
        }

        public void Solve()
        {
            // on resoud le VX:
            float vx = this.rb.PhysicStats.V.X;
            if (vx * this.lastVX < 0)// changement de signe
            {
                this.VXvariations++;
                this.VXtolerence = this.frameTolerence;
            }
            else
                this.VXtolerence--;
            this.lastVX = vx;
            if (this.VXvariations > this.frameLimit)
            {
                this.VXvariations = 0;
                this.rb.Sleeping = true;
                return;
            }
            else if (this.VXtolerence < 0)
            {
                this.VXtolerence = this.frameTolerence;
                this.VXvariations = 0;
            }
            // VY
            float vy = this.rb.PhysicStats.V.Y;
            if (vy * this.lastVY < 0)// changement de signe
            {
                this.VYvariations++;
                this.VYtolerence = this.frameTolerence;
            }
            else
                this.VYtolerence--;
            this.lastVY = vy;
            if (this.VYvariations > this.frameLimit)
            {
                this.VYvariations = 0;
                this.rb.Sleeping = true;
                return;
            }
            else if (this.VYtolerence < 0)
            {
                this.VYvariations = 0;
                this.VYtolerence = this.frameTolerence;
            }
            // angV
            float vang = this.rb.PhysicStats.AngularVitess;
            if (vang * this.lastVAng < 0)// changement de signe
            {
                this.AngVVariations++;
                this.AngVtolerence = this.frameTolerence;
            }
            else
                this.AngVtolerence--;
            this.lastVAng = vang;
            if (this.AngVVariations > this.frameLimit)
            {
                this.AngVVariations = 0;
                this.rb.Sleeping = true;
                return;
            }
            else if (this.AngVtolerence < 0)
            {
                this.AngVtolerence = this.frameTolerence;
                this.AngVVariations = 0;
            }
        }
    }
}
